package com.tueye.utils;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * Title        CreateHtmlUtil.java</br>
 * Description   <p>设置分页常用</p>
 * createTime    Jan 14, 2009 </br>
 * @author  yuyue
 * @version 1.0
 * 版权  ***
 * changetime  Jan 14, 2009   
 * lastchangeuser 
 */
public class CreateHtmlUtil {

	//设置分页
	public static String getPageNum(long currentNum, long totalNum,
			String topicEnglishName) {

		StringBuffer sb = new StringBuffer();
		if (totalNum > 1) {
			if (currentNum != 1) {
				sb.append("<a href=\"" + topicEnglishName + "_"
						+ (currentNum - 1) + ".html\">上一页</a>");
			} else {
				sb.append("<span class=\"disabled\"> 上一页 </span>");
			}
			if (totalNum <= 10) {

				for (int i = 1; i <= totalNum; i++) {
					if (i == currentNum) {
						sb.append("<span class=\"current\">" + i + "</span>");
					} else {
						sb.append("<a href=\"" + topicEnglishName + "_" + i
								+ ".html\">" + i + "</a>");
					}
				}
			} else {
				if (currentNum <= 5) {
					for (long i = 1; i <= currentNum; i++) {
						sb.append(i);
					}
					sb.append("...").append(totalNum - 1).append(totalNum);
				} else if ((totalNum - currentNum) <= 5) {
					sb.append(1).append(2).append("...");
					for (long i = currentNum - 1; i <= totalNum; i++) {
						sb.append(i);
					}
				} else {
					sb.append(1).append(2).append("...").append(currentNum - 1)
					.append(currentNum).append(currentNum + 1).append("...").append(totalNum - 1).append(totalNum);
				}
			}

			if (currentNum != totalNum) {
				sb.append("<a href=\"" + topicEnglishName + "_"+ (currentNum + 1) + ".html\">下一页</a>");
			} else {
				sb.append("<span class=\"disabled\"> 下一页 </span>");
			}
		}

		return sb.toString().replaceAll(topicEnglishName + "_" + 1 + ".html",
				"index.html");
	}

	public static String subString(String str, int length) {
		if(StringUtils.isEmpty(str)){
			return null;
		}
		if (str.length() <= length) {
			return str;
		} else {
			return str.substring(0, length - 2) + "...";
		}
	}
}
